//
//  ItemDetailBottomView.m
//  DanTang
//
//  Created by honeyrr on 2017/9/25.
//  Copyright © 2017年 honeyrr. All rights reserved.
//

#import "ItemDetailBottomView.h"
#import "CommentBottomView.h"
#import "CommentTableViewCell.h"

static NSString *ID = @"CommentTableViewCell";

@interface ItemDetailBottomView()<UIWebViewDelegate,CommentBottomViewDelegate,UITableViewDelegate, UITableViewDataSource>

//图文详情展示
@property(nonatomic, strong) UIWebView *webView;

//评论展示
@property(nonatomic, strong) UITableView *commentTableView;

//评论数组
@property(nonatomic, strong) NSArray *commentArr;

@property(nonatomic, strong) CommentBottomView *commentView;

@end

@implementation ItemDetailBottomView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.commentView];
        [self addSubview:self.webView];
        [self addSubview:self.commentTableView];
        
    }
    return self;
}

-(UIWebView *)webView
{
    if (!_webView) {
        UIWebView *wv = [[UIWebView alloc]init];
        //自动对页面进行缩放
        wv.scalesPageToFit = YES;
        wv.dataDetectorTypes = UIDataDetectorTypeAll;
        wv.delegate = self;
        _webView = wv;
    }
    return _webView;
}
//加载切换按钮
-(CommentBottomView *)commentView
{
    if (!_commentView) {
        CommentBottomView *commentBtmView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([CommentBottomView class]) owner:nil options:nil] lastObject];
        commentBtmView.delegate = self;
        _commentView = commentBtmView;
    }
    return _commentView;
}


-(void)descBtnClick
{
    self.webView.hidden = NO;
    self.commentTableView.hidden = YES;
}

-(void)commentBtnClick
{
    self.commentTableView.hidden = NO;
    self.webView.hidden = YES;
}

-(UITableView *)commentTableView
{
    if (!_commentTableView) {
        UITableView *tv = [[UITableView alloc]init];
        [tv registerNib:[UINib nibWithNibName:NSStringFromClass([CommentTableViewCell class]) bundle:nil] forCellReuseIdentifier:ID];
        tv.hidden = YES;
        tv.tableFooterView = [UIView new];
        tv.separatorStyle = UITableViewCellSeparatorStyleNone;
        tv.delegate = self;
        tv.dataSource = self;
        _commentTableView = tv;
    }
    return _commentTableView;
}

-(NSArray *)commentArr
{
    if (!_commentArr) {
        _commentArr = [NSArray array];
    }
    return _commentArr;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.commentArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[CommentTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.comment = self.commentArr[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64;
}

-(void)setItem:(NSDictionary *)item
{
    _item = item;
    //图文详情
    NSString *urlString = [NSString stringWithFormat:@"http://api.dantangapp.com/v2/items/%ld",[item[@"id"] integerValue]];
    [[DTNetWork sharedNetWork] loadDataInfo:urlString parameters:nil success:^(id  _Nullable responseObject) {
        NSLog(@"图文详情：%@", responseObject[@"data"][@"detail_html"]);
        NSString *detail_html = responseObject[@"data"][@"detail_html"];
        [self.webView loadHTMLString:detail_html baseURL:nil];
    } failure:^(NSError * _Nullable error) {
        
    }];
    
    //评论
    NSString *commentUrl = [NSString stringWithFormat:@"http://api.dantangapp.com/v2/items/%ld/comments", [item[@"id"] integerValue]];
    [[DTNetWork sharedNetWork] loadDataInfo:commentUrl parameters:nil success:^(id  _Nullable responseObject) {
        NSLog(@"responseObject:%@",responseObject);
        NSArray *dicts = responseObject[@"data"][@"comments"];
        NSMutableArray *comments = [NSMutableArray new];
        for (NSDictionary *dict in dicts) {
            NSDictionary *itemDict = @{@"content":dict[@"content"], @"created_at":dict[@"created_at"], @"avatar_url": dict[@"user"][@"avatar_url"], @"nickname":dict[@"user"][@"nickname"]};
            [comments addObject:itemDict];
        }
        self.commentArr = comments;
        [self.commentTableView reloadData];
    } failure:^(NSError * _Nullable error) {
        
    }];
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.commentView.frame = CGRectMake(0, 0, DTScreenW, 45);
    self.webView.frame = CGRectMake(0, 45, DTScreenW, self.mr_height - 45);
    self.commentTableView.frame = self.webView.frame;
}

-(void)webViewDidStartLoad:(UIWebView *)webView
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

@end
